package com.nacrtstudio.webgiftshop.pages.admin;

import java.util.List;

import javax.inject.Inject;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.EventLink;
import org.apache.tapestry5.corelib.components.Form;

import com.nacrtstudio.webgiftshop.facade.Controller;
import com.nacrtstudio.webgiftshop.model.shop.Category;
import com.nacrtstudio.webgiftshop.model.shop.beans.CategoryBean;

@RequiresRoles("admin")
public class AllCategories {
	@Inject
	private Controller controller;
	@Property
	private Category category;

	@InjectComponent
	private Form form;
	@Property
	private String categoryName;
	@Property
	private String description;
	
	public List<Category> getCategories(){
		return controller.getAllCategories();
	}
	
	public void onSubmitFromForm(){
		try {
			controller.createCategory(categoryName,description);
		} catch (Exception e) {
			System.out.println("\n\n\n\n"+e+"\n\n\n\n");
		}
	}
	
	
	@InjectPage
	private EditCategory editCategoryPage;
	
	public Object onEdit(Long id){
		Category c = controller.getCategory(id);
		editCategoryPage.setCategory(c);
		return editCategoryPage;
	}
	
	public void onDelete(Long id){
		try {
			controller.deleteCategory(id);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	@Component(parameters={"event=changeStatus","context=category.categoryId"})
	private EventLink changeStatusToInactive, changeStatusToAcitve;
	
	public void onChangeStatus(Long id){
		try {
			Category category = controller.getCategory(id);
			if (category.isStatus()) {
				category.setStatus(false);
				controller.updateCategory(category);
			} else {
				category.setStatus(true);
				controller.updateCategory(category);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
